package com.kfm.wiki.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kfm.wiki.entity.Content;
import com.kfm.wiki.entity.Doc;
import com.kfm.wiki.ex.BusinessException;
import com.kfm.wiki.ex.BusinessExceptionCode;
import com.kfm.wiki.mapper.ContentMapper;
import com.kfm.wiki.mapper.DocMapper;
import com.kfm.wiki.service.DocService;
import com.kfm.wiki.socket.WsService;
import com.kfm.wiki.util.IPUtil;
import com.kfm.wiki.util.PageReq;
import com.kfm.wiki.util.RedisUtil;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 10854
* @description 针对表【doc】的数据库操作Service实现
* @createDate 2022-10-18 14:14:16
*/
@Service
public class DocServiceImpl extends ServiceImpl<DocMapper, Doc>
    implements DocService{

    @Autowired
    private ContentMapper contentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private WsService wsService;

//    @Autowired
//    private RocketMQTemplate rocketMQTemplate;

    @Override
    public Page<Doc> page(Doc doc, PageReq page) {
        if (ObjectUtil.isEmpty(doc)){
            return null;
        }
        QueryWrapper<Doc> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(doc.getName())){
            wrapper.like("name", doc.getName());
        }
        wrapper.orderByAsc("sort");
        PageHelper.startPage(page.getPage(), page.getSize());
        Page<Doc> docs = (Page<Doc>) getBaseMapper().selectList(wrapper);
        return docs;
    }

    @Override
    public List<Doc> list(Doc doc) {
        if (ObjectUtil.isEmpty(doc)){
            return null;
        }
        QueryWrapper<Doc> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(doc.getName())){
            wrapper.like("name", doc.getName());
        }
        if (ObjectUtil.isNotNull(doc.getEbookId())){
            wrapper.eq("ebook_id", doc.getEbookId());
        }
        wrapper.orderByAsc("sort");
        return getBaseMapper().selectList(wrapper);
    }

    @Override
    public void increaseViewCount(Long id) {
        getBaseMapper().increaseViewCount(id);
    }

    @Override
    public void increaseVoteCount(Long id) {
        String remoteAddr = IPUtil.getRemoteAddr();

        if (RedisUtil.validateRepeat(redisTemplate, "DOC_VOTE" + remoteAddr + "_" + id, 5)){
            getBaseMapper().increaseVoteCount(id);
        } else {
            throw new BusinessException(BusinessExceptionCode.VOTE_REPEAT);
        }

        Doc doc = getBaseMapper().selectById(id);
        wsService.send("【" + doc.getName() + "】被点赞了！", MDC.get("LOG_ID"));

        // mq 生产消息
//        rocketMQTemplate.convertAndSend("VOTE_TOPIC", "【" + doc.getName() + "】被点赞了！");
    }



    @Override
    public void updateDocInfo() {
        getBaseMapper().updateEbookInfo();
    }

    @Override
    @Transactional
    public boolean save(Doc doc) {
        Content content = new Content();
        content.setContent(doc.getContent());

        if (ObjectUtil.isEmpty(doc.getId())){
            Long id = IdUtil.getSnowflakeNextId();
            doc.setId(id);
            content.setId(id);
            contentMapper.insert(content);
            return getBaseMapper().insert(doc) == 1;
        } else {
            content.setId(doc.getId());
            contentMapper.updateById(content);
            return getBaseMapper().updateById(doc) == 1;
        }
    }
}




